package newcoder_exp.justPractise;

import org.junit.Test;
import java.util.*;
import linked.*;

public class NC24 {
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null)
            return head;
            /* 
                res节点保证为待求解的序列的前一个节点
                即以及去重的序列的最后一个元素
            */
        ListNode res = new ListNode(), ans = res, cur;
        res.next = head;
        while (res.next != null && res.next.next != null) { //若有一个达成，根据res定义必然完成条件
            //将cur变为重复序列或者单一序列的最后一个元素
            cur = res.next;
            boolean deleteCur = false;
            while (cur != null && cur.next != null && cur.val == cur.next.val) {
                cur = cur.next;
                deleteCur = true; //进入次循环，表名必定cur存在重复元素
            }
            if (deleteCur) {
                res.next = cur.next;
            } else {
                res = cur;
            }
        }
        return ans.next;
    }

    @Test
    public void test() {
        ListNode head = ListNode.newListNodeFromArray(new int[] { 1, 2, 2 });
        head = deleteDuplicates(head);
        System.out.println(head);
    }
}
